package day042;


import util.音频;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class 客户端 extends Frame{

    TextArea ta = new TextArea(); //大框框
    TextField  tf = new TextField(); //小框框单行
    Button bt = new Button("input"); //按钮

    ObjectInputStream ois;  //准备一个东西的发送管道
    ObjectOutputStream oos; //
    Scanner sc = new Scanner(System.in);
    public void start() throws UnknownHostException, IOException {
        setLayout(null); //取消默认布局
        add(ta); //添加各个组件
        add(tf);
        add(bt);
        ta.setBounds(10,70,320,350); //各个组件设置位置和大小
        tf.setBounds(10,430,320,40);
        bt.setBounds(200,485,50,30);
        bt.addActionListener(new Action());//绑定监听器
        tf.addActionListener(new Action());
        setTitle("客户端");
        setSize(350,550);
        setVisible(true);

        Socket s = new Socket("192.168.215.111",4444);
        OutputStream out = s.getOutputStream();
        InputStream in = s.getInputStream();
        ois  = new ObjectInputStream(in);
        oos = new ObjectOutputStream(out);
        new 接收().start();

    }

    class Action implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            String text = tf.getText();  //获取下面的文字
            tf.setText(""); //清空文字
            ta.setText(text); //把文字粘贴到上面去
            User u = new User();
            u.isay = text;
            try {
                oos.writeObject(u);
                oos.flush();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }

    class 接收 extends Thread {
        public void run() {
            for (;;) {
                try {
                    User u = (User)ois.readObject();  //接收东西
                    ta.setText(u.intName+"说:"+u.isay);
                    new 音频("/music/西南林业大学白龙校区商学院 1.mp3").start();
                } catch (IOException | ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) throws IOException {
        new 客户端().start();
    }
}
